package com.example.pceb.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pceb.entity.SystemLog;
import com.example.pceb.mapper.SystemLogMapper;
import com.example.pceb.service.SystemLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class SystemLogServiceImpl extends ServiceImpl<SystemLogMapper, SystemLog> implements SystemLogService {

    // 记录日志
    @Override
    public void logToDatabase(String level, String logger, String message, String thread, String stackTrace) {
        try {
            SystemLog log = new SystemLog();
            log.setLevel(level);
            log.setLogger(logger);
            log.setMessage(message);
            log.setThread(thread);
            log.setStackTrace(stackTrace);
            log.setCreateTime(LocalDateTime.now());
            
            this.save(log);
        } catch (Exception e) {
            // 避免日志记录失败导致循环调用
            System.err.println("Failed to save log to database: " + e.getMessage());
        }
    }
} 